﻿using HAF.LogUtility;
using HAF.LogUtility.Assistantor;
using SoapNotify.Assistantor;
using SoapNotify.Model;
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace SoapNotify
{
    public partial class FrmApiMain : Form
    {
        public FrmApiMain()
        {
            InitializeComponent();
        }

        private readonly string defaultUrl = "http://127.0.0.1:9100/api/down";
        private readonly string defaultContent = "{   \"ID\" : \"ac395b108adf4f6799242573dbf529f2\",   \"Type\" : \"upload\",   \"Task\" : {\"Action\": \"Start\"}}";

        private void FrmApiMain_Load(object sender, EventArgs e)
        {
            InitControlValues();
        }

        private void InitControlValues()
        {
            cmbType.SelectedIndex = 0;
            cmbPostType.SelectedIndex = 0;
            cmbContentType.SelectedIndex = 0;
            this.txtSoapUrl.Text = defaultUrl;
            txtContent.Text = "";
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            //  DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")
            TaskData postData = new TaskData()
            {
                ID = Guid.NewGuid().ToString("N"),
                Type = cmbType.Text.ToLower(),
                Task = new Tack() { Action = "Start", RunDate = dateTimePicker1.Value.ToString("yyyy-MM-dd") }
            };
            string postContent = JsonAssistant.Serialize(postData);
            ShowMessage(postContent);
            string rtnMessage = WebHelper.PushWebRequest(txtSoapUrl.Text, cmbPostType.SelectedIndex == 0 ? "POST" : "GET", postContent);
        }

        #region 消息输出
        private void ShowPopTips(string message)
        {
            MessageBox.Show(message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        int _msgLen = 0;

        private void ShowClear()
        {
            txtMsg.Clear();
        }

        private void ShowMessage(string msg)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string>(ShowMessage), msg);
            }
            else
            {
                if (_msgLen >= 300)
                {
                    txtMsg.Clear();
                    //txtMessages.Text = "";
                    _msgLen = 0;
                }
                LogHelper.WriteDebugLogExt("SysMonitor", msg);

                txtMsg.Text = txtMsg.Text + (DateTime.Now.ToString() + " " + msg + "\r\n");
                txtMsg.SelectionStart = txtMsg.Text.Length;
                txtMsg.ScrollToCaret();

                _msgLen++;

            }
        }

        #endregion 消息输出

        private void btnTest_Click(object sender, EventArgs e)
        {
            string xmlOutPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "xml", "", "NOTIFY_Movie_2025043001.xml");

            if (Regex.IsMatch("20250529213000", @"^\\d+(\\.\\d+)?$"))
            {

            }

            DateTime dt = DateTime.ParseExact("19480101000000", "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
            //DateTime dt0 = DateTime.ParseExact("20992324235959", "yyyyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture);  20250211000000
            DateTime dt1 = DateTime.ParseExact("20990622095959", "yyyyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture);
            DateTime.TryParseExact("20250211000000", "yyyyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out DateTime dt3);
            string tt = dt3.ToString("D");
            if (dt.CompareTo(DateTime.Now) < 0)
            {
                if (dt1.CompareTo(DateTime.Now) < 0)
                {

                }
            }

            DiskAccessable();
            //CreateLinkFile();
            /*
            string beginTimeStr = string.Format("{0}T{1}", DateTime.Now.ToString("yyyy-MM-dd"), "20:00");
            DateTime runTime = DateTime.Parse(beginTimeStr);

            test(60);

            string xmlInPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "xml", "Cmsmapping_202505071503321131_QMZBcK_reply.xml");
            //string xmlOutPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "xml", "NOTIFY_Movie_2025043001.xml");

            //LogHelper.WriteInfoLogExt("testlog","test info");
            //LogHelper.WriteInfoLogExt("testlog", "test info {0}", xmlOutPath);
            //读取xml
            XmlAssistant.GetInstance().ReadReplyContent(xmlInPath, "readxml");
            //XmlAssistant.GetInstance().RenewPubXml(xmlInPath, xmlOutPath, "renewxml"); 
            //写Xml
            */
        }

        private void test(int expireTime)
        {
            DateTime bTime = DateTime.Now.AddDays(-expireTime);
            DateTime eTime = DateTime.Now.AddDays(-(expireTime + 10));
        }

        /// <summary>
        /// 存储可访问性校验
        /// </summary>
        private void DiskAccessable()
        {
            //NFS端口 4001 2049
            //Microsoft-DS(CIFS) 445 默认开放的SMB服务 CIFS协议（通用因特网文件系统协议）
            //139端口是基于SMB协议（服务器协议族）对外提供共享服务
            string filePath = "\\\\172.25.8.73\\worksheet\\FTP\\Resource\\cp\\Movie\\202503\\06\\db_mhqx13.mp4";
            string tt = Regex.Match(filePath, "\\d{6}").Value;
            int a = filePath.IndexOf(tt) + 6;
            string b = filePath.Substring(a, 3).Remove(0,1);
            string tmpDateStr = string.Concat(tt.Substring(0, 4),"-", tt.Substring(4, 2), "-", b);
            ShowMessage(filePath);
            if (File.Exists(filePath))
            {
                ShowMessage("文件存在");
            }
            else
            {
                ShowMessage("文件不存在");
            }
        }

        private void CreateLinkFile()
        {
            if (!ShortcutHelper.IsExists("mytest", @"D:\Temp\Panda4K\abc.exe"))
            {
                ShortcutHelper.CreateNewDesktopShortcut("mytest", @"D:\Temp\Panda4K\abc.exe", true);
            }
            else
            {
                ShowMessage("桌面快捷方式已存在");
            }
        }
    }
}
